# init env what a mess...
from os import path
current_path = path.abspath(path.dirname(__file__))
import_path = '/'.join(current_path.split('/')[:-1])
test_path = import_path + '/test'
import sys
sys.path.insert(0, import_path)
print import_path

from test.usedForTest1 import *
from test.usedForTest2 import *


from parse import ParseClass
from parse import ParseFile
from agentModel import ClassAgent
from agentModel import MethodAgent
from agentModel import AttrAgent
from writer import DotWriter

def test_ParseClass():
    p = ParseClass()
    ####################################
    ##### test.usedForTest1.SimpleClass1
    from test.usedForTest1 import SimpleClass1
    class_ = SimpleClass1

    res = p.parseClass(class_)
    assert isinstance(res, ClassAgent)
    assert res.getName() == 'test.usedForTest1.SimpleClass1'

    assert isinstance(res.getMethod(), list)
    assert len(res.getMethod()) == 2
    m1 = res.getMethod()[0]
    m2 = res.getMethod()[1]
    assert isinstance(m1, MethodAgent)
    assert isinstance(m2, MethodAgent)
    assert m1.getName() == 'haha'
    assert m2.getName() == 'parent'
    assert m1.getParam() == ['self']
    assert m2.getParam() == ['self', 'lalala']

    #assert isinstance(res.getAttr(), 
    assert len(res.getAttr()) == 1
    attr = res.getAttr()[0]
    assert isinstance(attr, AttrAgent)
    assert attr.getName() == 'ss'

    ####################################
    ##### test.usedForTest1.AnotherClass1
    from test.usedForTest1 import AnotherClass1
    class_ = AnotherClass1

    res = p.parseClass(class_)
    assert isinstance(res, ClassAgent)
    assert res.getName() == 'test.usedForTest1.AnotherClass1'
    assert len(res.getMethod()) == 1
    m = res.getMethod()[0]
    assert m.getName() == 'lala'
    assert m.getParam() == ['self', 'bulubulukaka']

    #####################################
    ##### test.usedForTest2.SimpleClass2
    from test.usedForTest2 import SimpleClass2
    class_ = SimpleClass2

    res = p.parseClass(class_)
    assert isinstance(res, ClassAgent)
    assert res.getName() == 'test.usedForTest2.SimpleClass2'

    assert isinstance(res.getMethod(), list)
    # just display the method defined in child class
    assert len(res.getMethod()) == 1
    m1 = res.getMethod()[0]
    assert isinstance(m1, MethodAgent)
    assert m1.getName() == 'haha'
    assert m1.getParam() == ['self']

    #assert isinstance(res.getAttr(), 
    # TODO seems shouldn't find ss here
    # still can find ss
    assert len(res.getAttr()) == 1
    attr = res.getAttr()[0]
    assert isinstance(attr, AttrAgent)
    assert attr.getName() == 'ss'


def test_ParseFile():
    p = ParseFile()
    file1 = test_path + '/usedForTest1.py'
    file2 = test_path + '/usedForTest2.py'
    res = p.parseFile([file1, file2])
    print res[0]
    print res[1]
    print res[2]

    assert len(res) == 3
    c1 = res[0]
    c2 = res[1]
    c3 = res[2]

    ######################################
    # for usedForTest1.AnotherClass1
    assert c1.getName() == 'test.usedForTest1.AnotherClass1'
    assert len(c1.getMethod()) == 1
    c1_m1 = c1.getMethod()[0]
    assert c1_m1.getName() == 'lala'
    assert c1_m1.getParam() == ['self', 'bulubulukaka']
    assert len(c1.getAttr()) == 0

    ######################################
    # for usedForTest1.SimpleClass1
    assert c2.getName() == 'test.usedForTest1.SimpleClass1'
    assert len(c2.getMethod()) == 2
    c2_m1 = c2.getMethod()[0]
    c2_m2 = c2.getMethod()[1]
    assert c2_m1.getName() == 'haha'
    assert c2_m2.getName() == 'parent'
    assert len(c2.getAttr()) == 1
    assert c2.getAttr()[0].getName() == 'ss'



    ######################################
    # for usedForTest2.SimpClass2
    assert c3.getName() == 'test.usedForTest2.SimpleClass2'
    # here don't display parent since it's an overriden method
    assert len(c3.getMethod()) == 1
    assert c3.getMethod()[0].getName() == 'haha'
    assert c3.getMethod()[0].getParam() == ['self']
    #TODO badthing.... should we display the attribute from parent?
    #print c3.getAttr()[0].getName()
    assert c3.getAttr()[0].getName() == 'ss'

if __name__ == '__main__':
#    test_new_parse()
#    test_dot()
#    test_agentHasMethodsNameList()
    test_ParseClass()
    test_ParseFile()

